"use strict";
/**
 * @author zhaojiuyi
 * @date 2022/5/25-21:55
 * @version 1.0.0
 */
import { Message } from 'element-ui';
export default {
  //深拷贝
  // 该方法能正确处理的对象只有 Number, String, Boolean, Array, 扁平对象，即那些能够被 json 直接表示的数据结构
  jsonToParse: (data) => {
    return JSON.parse(JSON.stringify(data));
  },
  successMessage: (msg) => {
    Message({
      message: msg,
      type: 'success'
    });
  },
  errorMessage: (msg) => {
    Message({
      message: msg,
      type: 'error'
    });
  },

  // axios流下载
  axiosDownload: (response)=>{
    const blob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
    const downloadElement = document.createElement('a');
    const href = window.URL.createObjectURL(blob);
    let contentDisposition = response.headers['content-disposition'];  //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
    let part = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
    let result = part.exec(contentDisposition);
    let filename = decodeURI(result[1]);
    downloadElement.style.display = 'none';
    downloadElement.href = href;
    downloadElement.download = filename ; //下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    return true;
  }

}

